<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15
 * Time: 9:55
 */

namespace app\index\controller;


use think\Db;

class Excel extends Common
{
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 * 导出库存excel
	 * @param int $id  店id号
	*/
	public function export()
	{
		$name = '库存表';
		$header = ['商品名称','条形码','库存量'];
		$b = 0;
		$sid = input('get.sid');
		$data = Db::table('inventory')
			->field('product.pname,product.barcode,inventory.number,inventory.sellnumber')
			->join('product','product.id = inventory.pid','LEFT')
			->where('inventory.sid',$sid)
			->select();
	
		foreach($data as $val){
			$newdata[$b]['pname']=$val['pname'];
			$newdata[$b]['barcode']=$val['barcode'];
			$newdata[$b]['number']=$val['number'] - $val['sellnumber'];
			$b++;
		}
		excelExport($name,$header,$newdata);
	}
	
	/**
	 * 导出商品表
	*/
	public function productExcel()
	{
		$name= '商品表';
		$header = ['编号','商家id号','产品名称','类型','进价(元)','售价(元)','条形码'];
		$b = 0;
		$data = Db::table('product')->where('status',7)->select();
		
		foreach($data as $val){
			$newdata[$b]['id'] = $val['id'];
			$newdata[$b]['bid'] = $val['bid'];
			$newdata[$b]['pname'] = $val['pname'];
			$newdata[$b]['category'] = $val['category'];
			$newdata[$b]['purchaseprice'] = $val['purchaseprice'];
			$newdata[$b]['retailprice'] = $val['retailprice'];
			$newdata[$b]['barcode'] = $val['barcode'];
			$b++;
		}
		excelExport($name,$header,$newdata);
	}
}